package com.wkbb.basic.controller.open;

import com.wkbb.basic.model.OrgServiceConfig;
import com.wkbb.basic.model.Organization;
import com.wkbb.basic.service.OrgServiceConfigService;
import com.wkbb.basic.service.OrganizationService;
import com.wkbb.basic.service.UserOpenService;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.ClassInfoDto;
import com.wkbb.common.dto.basic.OrgServiceConfigDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.enums.OrganizationTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luowenyan
 * @version V1.0
 * @className OrgServiceConfigController
 * @description TODO
 * @date 2019/8/19 12:01
 */
@RestController
@Api(description = "学校服务配置对外接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class OrgServiceConfigController {

    @Autowired
    private OrgServiceConfigService orgServiceConfigService;

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "根据orgId获取学校服务配置接口")
    @RequestMapping(value = "getOrgConfigByOrgId", method = RequestMethod.GET)
    public ResultDTO<OrgServiceConfigDto> getOrgConfigByOrgId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("***********OrgServiceConfigController.getOrgConfigByOrgId******apiVersion:{},orgId:{}", apiVersion, orgId);
        try {
            if (null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            OrgServiceConfig orgServiceConfig = orgServiceConfigService.getOrgConfigByOrgId(orgId);
            OrgServiceConfigDto orgServiceConfigDto;
            if (null != orgServiceConfig) {
                orgServiceConfigDto = new OrgServiceConfigDto();
                BeanUtils.copyProperties(orgServiceConfig, orgServiceConfigDto);
            } else {
                orgServiceConfigDto = OrgServiceConfigDto.getDefaultDto();
            }
            return new ResultDTO<>(orgServiceConfigDto);
        } catch (Exception e) {
            log.error("***********OrgServiceConfigController.getOrgConfigByOrgId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "保存学校服务配置信息")
    @RequestMapping(value = "saveOrgConfig", method = RequestMethod.POST)
    public ResultDTO<OrgServiceConfigDto> saveOrgConfig(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校服务配置信息", required = true) @RequestBody OrgServiceConfigDto orgServiceConfigDto
    ){
        log.info("***********OrgServiceConfigController.saveOrgConfig******apiVersion:{},orgId:{}", apiVersion, orgServiceConfigDto);
        try {
            if (null == orgServiceConfigDto.getOrgId()){ return new ResultDTO<>(ErrorCode.PARAM_MISSING);}

            Organization organization = organizationService.getByPrimaryKey(orgServiceConfigDto.getOrgId());
            if(null!=organization)
            {
                //如果不是幼儿园的，默认看宝宝关闭
                /*if(organization.getOrgType()!= OrganizationTypeEnum.KINDERGARTEN.getValue().intValue())
                {
                    orgServiceConfigDto.setVideoSwitch(false);
                }

                //如果是幼儿园的，默认体育关闭
                if(organization.getOrgType()== OrganizationTypeEnum.KINDERGARTEN.getValue().intValue())
                {
                   // orgServiceConfigDto.setSportsSwitch(false);
                }*/
            }

            OrgServiceConfig orgServiceConfig = new OrgServiceConfig();
            BeanUtils.copyProperties(orgServiceConfigDto , orgServiceConfig);
            orgServiceConfigService.saveOrgServiceConfig(orgServiceConfig, orgServiceConfigDto.getUserDto());
            return new ResultDTO<>(true);
        } catch (Exception e) {
            log.error("***********OrgServiceConfigController.saveOrgConfig******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
